<template>
	<view class="mainpadding">
		<view class="sishitext">输入手机号</view>
		<view class="margin_top7">
			<input class="inputs" type="number" placeholder="请输入您的手机号码" v-model="mobile">
		</view>
		<view class="margin_top inputs flexbetween">
			<input class="kuandu" type="password" placeholder="请输入手机验证码" v-model="captcha">
			<view class="huangsetext" @click="sendcode">{{codetext}}</view>
		</view>
		<view class="margin_top">
			<input class="inputs" type="password" placeholder="请输入密码" v-model="passwordtext">
		</view>
		<view class="margin_top">
			<input class="inputs" type="password" placeholder="请再次输入密码" v-model="newpassword">
		</view>
		<view class="margin_top7 bigbtn" @click="over">完成</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				mobile:"",
				captcha:"",
				passwordtext:"",
				newpassword:"",
				codetext: "获取验证码",
			}
		},
		methods: {
			// 发送验证码
			sendcode() {
				let _this = this
				let mobile = _this.mobile;
				if (!mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				httpRequest.request('/api/sms/send', 'GET', {
					mobile: mobile,
					event: "resetpwd"
				}).then(resp => {
					if (resp.code == 1) {
						var i = 60;
						var timer = setInterval(function() {
							_this.codetext = i + "秒";
							if (i < 1) {
								clearInterval(timer);
								_this.codetext = "获取验证码";
							} else {
								i--;
							}
						}, 1000)
			
					} else if (resp.code == 0) {
						httpRequest.toast(resp.msg);
					} else {
						httpRequest.toast('网络开小差啦');
					}
				})
			},
			// 注册
			over() {
				let _this = this
				let mobile = _this.mobile;
				
				if (mobile == '') {
					httpRequest.toast('请输入手机号');
					return false;
				}
				if (!mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				if (_this.captcha == '') {
					httpRequest.toast("请输入验证码");
					return false;
				}
				if (_this.passwordtext == '') {
					httpRequest.toast("请输入密码");
					return false;
				}
				if (_this.newpassword == '') {
					httpRequest.toast("请输入确认密码");
					return false;
				}
				if (_this.passwordtext != _this.newpassword) {
					httpRequest.toast("两次输入密码不一致,请重新输入")
					return false
				}
				httpRequest.request('/api/user/resetpwd', 'POST', {
					mobile: _this.mobile,
					newpassword: _this.newpassword,
					captcha: _this.captcha
				}).then(res => {
					if (res.code == 1) {
						httpRequest.toast(res.msg);
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							});
						}, 2000)
					} else if (res.code == 0) {
						httpRequest.toast(res.msg);
					} else {
						httpRequest.toast('网络开小差啦');
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.kuandu {
		width: 60%;
	}

	.jiutext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.huangsetext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FAB536;
	}

	.huisetext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.ershib {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
	}

	.inputs {
		padding-bottom: 30rpx;
		border-bottom: 2rpx solid rgba(153, 153, 153, 0.1);
	}

	.sishitext {
		font-size: 40rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #000000;
	}

	.denglbox {
		padding: 208rpx 30rpx 0;
	}
</style>
